﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace SecurityControl
{
	/// <summary>
	/// Summary description for UserControl1.
	/// </summary>
	public class SecurityControl : System.Windows.Forms.UserControl
	{
		string str_Value; 

		string str_chkHienThi = "0"; 
		string str_chkThem = "0"; 
		string str_chkSua = "0"; 
		string str_chkXoa = "0";
		string str_chkKichHoat = "0";
		public delegate void CheckBoxClickedHandler();

		private Infragistics.Win.UltraWinEditors.UltraCheckEditor chkHienThi;
		private Infragistics.Win.UltraWinEditors.UltraCheckEditor chkThemMoi;
		private Infragistics.Win.UltraWinEditors.UltraCheckEditor chkSua;
		private Infragistics.Win.UltraWinEditors.UltraCheckEditor chkXoa;
		private Infragistics.Win.UltraWinEditors.UltraCheckEditor chkKichHoat;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SecurityControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		[Category("Action")]
		[Description("Xãy ra khi các checkbox phân quyền kích hoạt.")]
		public event CheckBoxClickedHandler CheckBoxClicked;

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			Infragistics.Win.Appearance appearance1 = new Infragistics.Win.Appearance();
			this.chkHienThi = new Infragistics.Win.UltraWinEditors.UltraCheckEditor();
			this.chkThemMoi = new Infragistics.Win.UltraWinEditors.UltraCheckEditor();
			this.chkSua = new Infragistics.Win.UltraWinEditors.UltraCheckEditor();
			this.chkXoa = new Infragistics.Win.UltraWinEditors.UltraCheckEditor();
			this.chkKichHoat = new Infragistics.Win.UltraWinEditors.UltraCheckEditor();
			this.SuspendLayout();
			// 
			// chkHienThi
			// 
			appearance1.FontData.Name = "Tahoma";
			this.chkHienThi.Appearance = appearance1;
			this.chkHienThi.Location = new System.Drawing.Point(8, 8);
			this.chkHienThi.Name = "chkHienThi";
			this.chkHienThi.Size = new System.Drawing.Size(64, 20);
			this.chkHienThi.TabIndex = 0;
			this.chkHienThi.Text = "Hiển thị";
			this.chkHienThi.CheckedChanged += new System.EventHandler(this.chkHienThi_CheckedChanged);
			// 
			// chkThemMoi
			// 
			this.chkThemMoi.Location = new System.Drawing.Point(72, 8);
			this.chkThemMoi.Name = "chkThemMoi";
			this.chkThemMoi.Size = new System.Drawing.Size(72, 20);
			this.chkThemMoi.TabIndex = 0;
			this.chkThemMoi.Text = "Thêm mới";
			this.chkThemMoi.CheckedChanged += new System.EventHandler(this.chkThemMoi_CheckedChanged);
			// 
			// chkSua
			// 
			this.chkSua.Location = new System.Drawing.Point(144, 8);
			this.chkSua.Name = "chkSua";
			this.chkSua.Size = new System.Drawing.Size(48, 20);
			this.chkSua.TabIndex = 0;
			this.chkSua.Text = "Sửa";
			this.chkSua.CheckedChanged += new System.EventHandler(this.chkSua_CheckedChanged);
			// 
			// chkXoa
			// 
			this.chkXoa.Location = new System.Drawing.Point(192, 8);
			this.chkXoa.Name = "chkXoa";
			this.chkXoa.Size = new System.Drawing.Size(48, 20);
			this.chkXoa.TabIndex = 0;
			this.chkXoa.Text = "Xóa";
			this.chkXoa.CheckedChanged += new System.EventHandler(this.chkXoa_CheckedChanged);
			// 
			// chkKichHoat
			// 
			this.chkKichHoat.Location = new System.Drawing.Point(240, 8);
			this.chkKichHoat.Name = "chkKichHoat";
			this.chkKichHoat.Size = new System.Drawing.Size(72, 20);
			this.chkKichHoat.TabIndex = 0;
			this.chkKichHoat.Text = "Kích hoạt";
			this.chkKichHoat.CheckedChanged += new System.EventHandler(this.chkKichHoat_CheckedChanged);
			// 
			// SecurityControl
			// 
			this.BackColor = System.Drawing.SystemColors.HighlightText;
			this.Controls.Add(this.chkHienThi);
			this.Controls.Add(this.chkThemMoi);
			this.Controls.Add(this.chkSua);
			this.Controls.Add(this.chkXoa);
			this.Controls.Add(this.chkKichHoat);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "SecurityControl";
			this.Size = new System.Drawing.Size(312, 32);
			this.Load += new System.EventHandler(this.SecurityControl_Load);
			this.ResumeLayout(false);

		}
		#endregion

		public string Value 
		{ 
			get 
			{ 
				return str_Value; 
			} 
			set 
			{ 
				str_Value = value; 
				for(int i=0;i<=4;i++)
				{
					switch(i)
					{
						case 0:
							str_chkHienThi = str_Value.Substring(0,1);
							break;
						case 1:
							str_chkThem = str_Value.Substring(1,1);
							break;
						case 2:
							str_chkSua = str_Value.Substring(2,1);
							break;
						case 3:
							str_chkXoa = str_Value.Substring(3,1);
							break;
						case 4:
							str_chkKichHoat = str_Value.Substring(4,1);
							break;
					}
				}
				ChangeChecked(); 
			} 
		}
		private void ChangeChecked() 
		{ 
			if (str_chkHienThi == "1") 
			{ 
				chkHienThi.Checked = true; 
			} 
			else 
			{ 
				chkHienThi.Checked = false; 
			} 
			if (str_chkThem == "1") 
			{ 
				chkThemMoi.Checked = true; 
			} 
			else 
			{ 
				chkThemMoi.Checked = false; 
			} 
			if (str_chkSua == "1") 
			{ 
				chkSua.Checked = true; 
			} 
			else 
			{ 
				chkSua.Checked = false; 
			} 
			if (str_chkXoa == "1") 
			{ 
				chkXoa.Checked = true; 
			} 
			else 
			{ 
				chkXoa.Checked = false; 
			} 
			if (str_chkKichHoat == "1") 
			{ 
				chkKichHoat.Checked = true; 
			} 
			else 
			{ 
				chkKichHoat.Checked = false; 
			} 
		}

		private void SecurityControl_Load(object sender, System.EventArgs e)
		{
			if(str_Value!=null)
			{
				for(int i=0;i<=4;i++)
				{
					switch(i)
					{
						case 0:
							str_chkHienThi = str_Value.Substring(0,1);
							break;
						case 1:
							str_chkThem = str_Value.Substring(1,1);
							break;
						case 2:
							str_chkSua = str_Value.Substring(2,1);
							break;
						case 3:
							str_chkXoa = str_Value.Substring(3,1);
							break;
						case 4:
							str_chkKichHoat = str_Value.Substring(4,1);
							break;
					}
				}
			}
		this.ValueChange();
        this.ChangeChecked();
		}
		private void ValueChange()
		{
			str_Value = str_chkHienThi + str_chkThem + str_chkSua + str_chkXoa + str_chkKichHoat;
		}
		protected virtual void OnCheckBoxClicked()
		{
			if (CheckBoxClicked != null)
			{
				CheckBoxClicked();				
			}
		}

		private void chkHienThi_CheckedChanged(object sender, System.EventArgs e)
		{
			if (chkHienThi.Checked == true) 
			{ 
				str_chkHienThi = "1"; 
			} 
			else 
			{ 
				str_chkHienThi = "0"; 
			}
			this.ValueChange();
			this.OnCheckBoxClicked();
		}
		private void chkThemMoi_CheckedChanged(object sender, System.EventArgs e)
		{
			if (chkThemMoi.Checked == true) 
			{ 
				str_chkThem = "1"; 
			} 
			else 
			{ 
				str_chkThem = "0"; 
			}
			this.ValueChange();
			this.OnCheckBoxClicked();
		}
		private void chkSua_CheckedChanged(object sender, System.EventArgs e)
		{
			if (chkSua.Checked == true) 
			{ 
				str_chkSua = "1"; 
			} 
			else 
			{ 
				str_chkSua = "0"; 
			}
			this.ValueChange();
			this.OnCheckBoxClicked();
		}
		private void chkXoa_CheckedChanged(object sender, System.EventArgs e)
		{
			if (chkXoa.Checked == true) 
			{ 
				str_chkXoa = "1"; 
			} 
			else 
			{ 
				str_chkXoa = "0"; 
			}
			this.ValueChange();
			this.OnCheckBoxClicked();
		}
		private void chkKichHoat_CheckedChanged(object sender, System.EventArgs e)
		{
			if (chkKichHoat.Checked == true) 
			{ 
				str_chkKichHoat = "1"; 
			} 
			else 
			{ 
				str_chkKichHoat = "0"; 
			}
			this.ValueChange();
			this.OnCheckBoxClicked();
		}
		
	}
}
